<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品订单</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <!--工具条-->
        <el-form
          :inline="true"
          :model="searchfrom"
          class="toolbar"
          label-position="right"
          label-width="70px"
        >
          <el-row>
            <el-form-item label="订单号">
              <el-input v-model="searchfrom.queryString"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="searchfrom.queryType" placeholder="请选择" clearable>
                <el-option
                  v-for="item in statusLits"
                  :label="item.name"
                  :key="item.status"
                  :value="item.status"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label>
              <el-button @click="searchUserList" type="success" style="width: 170px">查询</el-button>
            </el-form-item>
          </el-row>
        </el-form>
      </el-row>
      <el-row>
        <el-table :data="gridData">
          <el-table-column property="orderNo" label="订单" width="150"></el-table-column>
          <el-table-column property="name" label="名称"></el-table-column>
          <el-table-column property="money" label="USDT"></el-table-column>
          <!-- <el-table-column property="oneMoneyCy" label="CY单价" ></el-table-column>
          <el-table-column property="cyMoney" label="CY总价" ></el-table-column>-->
          <el-table-column property="number" label="数量"></el-table-column>
          <el-table-column property="status" label="状态">
            <template slot-scope="scope">
              <span>{{getuserstatus(scope.row.status)}}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="success"
                size="mini"
                @click="edit(scope.row.id)"
                v-if="scope.row.status == 1"
              >发货</el-button>
              <el-button
                type="danger"
                size="mini"
                @click="open(scope.row.id)"
                v-if="scope.row.status == 1"
              >失效</el-button>
              <el-button
                type="success"
                size="mini"
                @click="agreeReturn(scope.row.id)"
                v-if="scope.row.status == 4 || scope.row.status == 6"
              >同意{{scope.row.status==4?'退货':'退款'}}</el-button>
              <el-button
                type="danger"
                size="mini"
                @click="refuseReturn(scope.row.id)"
                v-if="scope.row.status == 4 || scope.row.status == 6"
              >拒绝{{scope.row.status==4?'退货':'退款'}}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchfrom.pageNo"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="searchfrom.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <el-dialog title="订单发货" :visible.sync="dialogFormVisible">
      <el-form :model="editform">
        <!-- <el-form-item label="物流公司" :label-width="formLabelWidth">
          <el-input v-model="editform.courierCompany" autocomplete="off"></el-input>
        </el-form-item>-->
        <el-form-item label="快递单号" :label-width="formLabelWidth">
          <el-input v-model="editform.queryString" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editformsubmit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="订单发货" :visible.sync="dialogFormVisibleRerurn">
      <el-form :model="editform">
        <!-- <el-form-item label="物流公司" :label-width="formLabelWidth">
          <el-input v-model="editform.courierCompany" autocomplete="off"></el-input>
        </el-form-item>-->
        <el-form-item label="拒绝原因" :label-width="formLabelWidth">
          <el-input v-model="returnForm.queryString" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleRerurn = false">取 消</el-button>
        <el-button type="primary" @click="returnSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      returnForm: {
        queryString: "",
        queryId: "",
        refresh:2
      },
      editform: {
        queryString: "",
        queryId: "",
      },
      dialogFormVisible: false,
      gridData: [],
      searchfrom: {
        pageNo: 1,
        pageSize: 10,
        queryString: "",
        queryType: -1,
      },
      statusLits: [
        {
          status: -1,
          name: "请选择",
        },
        {
          status: 0,
          name: "待支付",
        },
        {
          status: 1,
          name: "待发货",
        },
        {
          status: 2,
          name: "待收货",
        },

        {
          status: 3,
          name: "已完成",
        },
        {
          status: 4,
          name: "退货申请中",
        },
        {
          status: 5,
          name: "退货中",
        },
        {
          status: 6,
          name: "待退款",
        },
        {
          status: 7,
          name: "退货退款完成",
        },
        {
          status: 8,
          name: "已失效",
        },
      ],
      total: "",
      dialogFormVisibleRerurn: false,
    };
  },
  created() {
    this.getcatelist();
  },
  methods: {
    refuseReturn(row) {
      this.dialogFormVisibleRerurn = true;
      this.returnForm.queryId = row;
    },
    returnSubmit() {
      if (!this.returnForm.queryString) {
        this.$message({
          type: "warning",
          message: "请输入拒绝原因",
        });
        return;
      }
      this.$http.post("/shop/agreeReturn", this.returnForm).then((res) => {
        if (res.data.status == 1) {
          this.$message({
            type: "success",
            message: "提交成功",
          });
          this.dialogFormVisibleRerurn=false;
          this.getcatelist();
        } else {
          this.$message({
            type: "warning",
            message: res.data.message,
          });
        }
      });
    },
    agreeReturn(row) {
      let message=row==4?'退货':'退款';
      this.$confirm("确定同意"+message+"?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .post("/shop/agreeReturn", {
              queryId: row,
              refresh:1
            })
            .then((res) => {
              if (res.data.status == 1) {
                this.$message({
                  type: "success",
                  message: "提交成功",
                });
                this.getcatelist();
              } else {
                this.$message({
                  type: "warning",
                  message: res.data.message,
                });
              }
            });
        })
        .catch(() => {});
    },
    ///shop/agreeReturn
    editformsubmit() {
      if (!this.editform.queryString) {
        this.$message({
          type: "warning",
          message: "请输入物流单号",
        });
        return;
      }
      this.$http.post("/shop/goodPush", this.editform).then((res) => {
        console.log(res);
        if (res.data.status == 1) {
          this.dialogFormVisible = false;
          this.$message({
            type: "success",
            message: "提交成功",
          });
          this.getcatelist();
        } else {
          this.$message({
            type: "info",
            message: res.data.message,
          });
        }
      });
    },
    open(row) {
      this.$confirm("修改订单状态为，已失效, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .post("/shop/invalidOrder", {
              queryId: row,
            })
            .then((res) => {
              console.log(res);
              if (res.data.status == 1) {
                this.$message({
                  type: "success",
                  message: "修改成功!",
                });
                this.getcatelist();
              } else {
                this.$message({
                  type: "warning",
                  message: res.data.message,
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    edit(row) {
      this.editform.queryId = row;
      this.dialogFormVisible = true;
    },
    searchUserList() {
      this.getcatelist();
    },
    getuserstatus(row) {
      let re = "";
      this.statusLits.forEach((obj) => {
        if (obj.status == row) {
          re = obj.name;
        }
        return;
      });
      return re;
    },
    getcatelist() {
      // if()
      this.$http.post("/shop/orderList", this.searchfrom).then((res) => {
        //   console.log(res)
        this.gridData = res.data.data.list;
        this.total = res.data.data.total;
        console.log(this.gridData);
      });
    },
    // 监听 pagesize 的变化
    handleSizeChange(newSize) {
      // 把最新的 pagesize 赋值给 this.queryinfo
      this.searchfrom.pageSize = newSize;
      this.getcatelist();
    },
    // 监听 页码值 的变化
    handleCurrentChange(newPageNum) {
      console.log(newPageNum);
      this.searchfrom.pageNo = newPageNum;
      this.getcatelist();
    },
    // POST /manage/mall/getMallOrder
  },
};
</script>
